#include "iot.h"
#include "coap.h"

#include "at.h"
#include "system.h"
#include "string_util.h"

#define IOTDebug(format,...)  printf("[IOT] "format, ##__VA_ARGS__)
#define IOTDebugln(format,...) printf("[IOT] "format"\n",##__VA_ARGS__)


coap_variable_t Package;
uint8_t         Space[512];



void Iot_SetAccount(char* name, char* passwd)
{
	strcpy(UserName, name);	
	strcpy(Password, passwd);
	attach_variable_part(&Package, 512, Space);
}






void Iot_Register(void)
{
	uint8_t space[64];
	
	joint_vtcid(&Package, 1, COAP_MSG_CON, 
		COAP_CODE_REQUEST | COAP_REQUEST_POST, 
		0x01);
	
	space[0]  = 0x2e;
	joint_option(&Package, 11, 1, space);
	
	space[0]  = 0x27;
	space[1]  = 0x11;
	joint_option(&Package, 1, 2, space);
	
	space[0]  = 0x00;
	space[1]  = 0x02;
	joint_option(&Package, 267 -12, 2, space);
	
	sprintf((char*)space, "{\"api\":\"%s\",\"lbl\":\"%s\"}",
		UserName, Password);
	
//	IOTDebugln("Reg info:%s",  (char*)space);
	
	joint_payload(&Package, strlen((char*)space), space);
	
	// TODO
	sprintf( (char*)space, "AT+QISEND=%d", Package.size );
	sendAtCmd( (char*)space);

//	IOTDebugln("%s\r\n", (char*)space);
	
	delayMs(500);
	sendAtData( Package.data, Package.size);
}



void Iot_UploadDeviceStringAttribute(char* dev_name, char* dev_attr, char* param)
{
	uint8_t space[64];
	
	IOTDebugln("%s,%s,%s", dev_name, dev_attr, param);

	joint_vtcid(&Package, 1, COAP_MSG_CON,
		COAP_CODE_REQUEST | COAP_REQUEST_POST, 0x02);  
	
	sprintf( (char*)space, "~/%s", dev_attr);	
	joint_option(&Package, 11, strlen( (char*)space), space);
	
//	IOTDebugln("Uri path:%s", (char*)space);
	
	space[0]  = 0x00;
	joint_option(&Package, 1, 1, space);
	
	sprintf( (char*)space, "./%s", dev_name);
	joint_option(&Package, 256 -1 -11, strlen( (char*)space), space);
	
//	IOTDebugln("From :%s", (char*)space);
	
	space[0]  = 0x00;
	space[1]  = 0x04;
	joint_option(&Package, 267 -256, 2, space);
	
	joint_payload(&Package, strlen(param), (uint8_t*)param);
	
	// TODO
	sprintf( (char*)space, "AT+QISEND=%d", Package.size );
	sendAtCmd( (char*)space);

	IOTDebugln("%s\r\n", (char*)space);
	
	delayMs(500);
	sendAtData( Package.data, Package.size);	
}



void Iot_UploadDeviceIntAttribute(char* dev_name, char*dev_attr, int param)
{
	uint8_t space[64];
	
	IOTDebugln("%s,%s,%d", dev_name, dev_attr, param);

	joint_vtcid(&Package, 1, COAP_MSG_CON,
		COAP_CODE_REQUEST | COAP_REQUEST_POST, 0x02);  
	
	sprintf( (char*)space, "~/%s", dev_attr);	
	joint_option(&Package, 11, strlen( (char*)space), space);
	
	space[0]  = 0x00;
	joint_option(&Package, 1, 1, space);
	
	sprintf( (char*)space, "./%s", dev_name);
	joint_option(&Package, 256 -1 -11, strlen( (char*)space), space);
	
	
	space[0]  = 0x00;
	space[1]  = 0x04;
	joint_option(&Package, 267 -256, 2, space);
	
	sprintf( (char*)space, "%d", param);
	joint_payload(&Package, strlen( (char*)space), space);	
	
	Package.data[Package.size]  = '\0';
	
//	
//	printf("\r\n***");
//	dump_mem(Package.data, Package.size);
	//IOTDebugln("%s\r\n", (char*)space);
	// TODO	
	
	
	
	sprintf( (char*)space, "AT+QISEND=%d", Package.size );
	sendAtCmd( (char*)space);

	
	
	delayMs(500);
	sendAtData( Package.data, Package.size);
	
}